home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / PERMUTE.TST / PERMUTST.C < prev    next >
C/C++ Source or Header  |  1996-02-29  |  4KB  |  120 lines

  1. /* ============ */
  2. /* permutst.c    */
  3. /* ============ */
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <miscdefs.h>
  9. #include <pmtndefs.h>
  10. #include <mconf.h>
  11.  
  12. #define    NUM_PROBS    100
  13. #define LOW_PROB    1.0e-8
  14.  
  15. static    PRMUT_DATA_STRU  PermuteData;
  16. static    INIT_DATA_STRU   InitialData;
  17.  
  18. static    double    ChiSqProb[NUM_PROBS];
  19. /* ==================================================================== */
  20. /* PermutationTest - Executes Permutation Test per Knuth, Vol 2, p. 64    */
  21. /* ==================================================================== */
  22. void
  23. main(void)
  24. {
  25.     int        k;
  26.     double  DegFree, LoLimit, HiLimit;
  27.     double  KnMinusProb, KnMinusStat, KnPlusProb, KnPlusStat;
  28.  
  29.     AbortGracefully();            /* Make ^C act reasonably */
  30.  
  31.     printf("\tP E R M U T A T I O N  T E S T\n\n");
  32.     GetInitialData(&InitialData);
  33.     fprintf(stderr, "\n"); fflush(NULL);
  34.  
  35.     /* -------------------------- */
  36.     /* Print Initial Data Entries */
  37.     /* -------------------------- */
  38.     printf("Starting Seed = %u%s\n", InitialData.UserSeed,
  39.     (InitialData.SeedSrce == (UINT)(-1)) ?
  40.         " (Unsigned Integer Part of Time of Day)" : "");
  41.  
  42.     printf("Generator     = %s\n", InitialData.GenName);
  43.  
  44.     /* -------------------------------------- */
  45.     /* Set Address of Random Number Generator */
  46.     /* -------------------------------------- */
  47.     PermuteData.RandFun = InitialData.RandFun;
  48.  
  49.     /* -------------------------------------------------- */
  50.     /* Initialize Control Parameters for Permutation Test */
  51.     /* -------------------------------------------------- */
  52.     SetPermuteControls(&PermuteData);
  53.  
  54.     /* --------------------------- */
  55.     /* Clear Random-Number Counter */
  56.     /* --------------------------- */
  57.     PermuteData.NumVariates = 0;
  58.  
  59.     printf("%6lu  Random Numbers Will be Generated (Minimum)\n\n",
  60.     (ULONG) PermuteData.NumObs * PermuteData.NumElements * NUM_PROBS);
  61.  
  62.     DegFree = PermuteData.NumCategories - 1;
  63.     ChiSqDist(LOW_PROB, DegFree, &LoLimit, &HiLimit);
  64.  
  65.     /* ------------------------- */
  66.     /* Generate Random Numbers,  */
  67.     /* Calculate Chi-Square Data */
  68.     /* ------------------------- */
  69.     fflush(NULL);
  70.     for (k = 0; k < NUM_PROBS; ++k)
  71.     {
  72.     CalcPermuteChiSq(&PermuteData);
  73.  
  74.     fprintf(stderr, "\rPass %3d (of %d), %8ld  Total Random Numbers",
  75.         k+1, NUM_PROBS, PermuteData.NumVariates);
  76.  
  77.     if (PermuteData.PrmutChiSq < LoLimit)
  78.     {
  79.           ChiSqProb[k] = LOW_PROB;
  80.     }
  81.     else if (PermuteData.PrmutChiSq > HiLimit)
  82.     {
  83.           ChiSqProb[k] = 1.0 - LOW_PROB;
  84.     }
  85.     else
  86.     {
  87.         ChiSqProb[k] = chdtr(DegFree, PermuteData.PrmutChiSq);
  88.     }
  89.  
  90.     if (ChiSqProb[k] < 0)
  91.     {
  92.         fprintf(stderr, "\nChiSqFreq(): Function chdtr() "
  93.         "Returned Negative Probability -  Can't Happen.\n");
  94.     }
  95.     P(printf("Permute Chi-Square Statistic = %f\n",
  96.        PermuteData.PrmutChiSqStat));
  97.     P(printf("Chi-Square Probability on %.f"
  98.         " Degrees of Freedom = %.4f\n", DegFree, ChiSqProb[k]));
  99.     }
  100.     /* -------------------------------------------------------- */
  101.     /* Calculate K-S on Chi-Square Statistics and Probabilities */
  102.     /* -------------------------------------------------------- */
  103.     fflush(NULL);
  104.     KSCalc(ChiSqProb, NUM_PROBS,
  105.         &KnPlusStat, &KnPlusProb,
  106.          &KnMinusStat, &KnMinusProb);
  107.  
  108.     printf("\nResults of Kolmogorov-Smirnov Test "
  109.         "on %d Chi-Square Probabilities:\n", NUM_PROBS);
  110.  
  111.     printf("\tK(%d)+ = %f (Knuth) or %9f%%\n", NUM_PROBS,
  112.     sqrt((double)NUM_PROBS)*KnPlusStat, 100*KnPlusProb);
  113.  
  114.     printf("\tK(%d)- = %f (Knuth) or %9f%%\n", NUM_PROBS,
  115.     sqrt((double)NUM_PROBS)*KnMinusStat, 100*KnMinusProb);
  116.  
  117.     printf("\nThis Run Required %ld Random Numbers\n",
  118.     PermuteData.NumVariates);
  119. }
  120.